### CODE FOR:
###
### Partisan Influence on Policy Preferences 
### When Parties and Voters Disagree on Welfare Retrenchment
###
### PART 1: Data preparations
###
### VERSION: 2023-MAY-30


rm(list=ls())
gc()



# WORKING DIRECTORY -------------------------------------------------------
setwd("C:/Users/miros/Desktop/Research/Finland - Partisan Cues in Citizens Attitudes")



# LIBRARIES ---------------------------------------------------------------
library(haven)




# DATA IMPORT -------------------------------------------------------------
# NOTE: The data file is available in the depository
BIBU.panel <- read_sav("01-data/BIBU panel data.sav")



# VARIABLES ---------------------------------------------------------------

# _ Party vote ------------------------------------------------------------

# ___ Round 1 -------------------------------------------------------------
# QUESTION WORDING: If the parliamentary elections were held now, 
#                   which party would you vote for?
BIBU.panel$PartyChoice.R1                      <- NA
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  1] <- "National Coalition"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  2] <- "Social Democrats"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  3] <- "True Finns"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  4] <- "Centre Party"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  5] <- "Left Alliance"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  6] <- "Greens"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  7] <- "Swedish Peoples Party"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  8] <- "Christian Democrats"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 ==  9] <- "Movement Now"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 == 10] <- "Other"
BIBU.panel$PartyChoice.R1[BIBU.panel$Q4 == 11] <- "Non-voter"

BIBU.panel$PartyChoice.R1 <- factor(BIBU.panel$PartyChoice.R1, 
                                    levels = c("Non-voter",
                                               "National Coalition",
                                               "Social Democrats",
                                               "True Finns",
                                               "Centre Party",
                                               "Left Alliance",
                                               "Greens",
                                               "Swedish Peoples Party",
                                               "Christian Democrats",
                                               "Movement Now",
                                               "Other"))


# ___ Round 2 -------------------------------------------------------------
# QUESTION WORDING: If the parliamentary elections were held now, 
#                   which party would you vote for?
BIBU.panel$PartyChoice.R2                      <- NA
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  1] <- "National Coalition"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  2] <- "Social Democrats"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  3] <- "True Finns"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  4] <- "Centre Party"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  5] <- "Left Alliance"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  6] <- "Greens"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 == 12] <- "Swedish Peoples Party"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  7] <- "Christian Democrats"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  8] <- "Movement Now"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 ==  9] <- "Other"
BIBU.panel$PartyChoice.R2[BIBU.panel$Q6_R2 == 10] <- "Non-voter"

BIBU.panel$PartyChoice.R2 <- factor(BIBU.panel$PartyChoice.R2, 
                                    levels = c("Non-voter",
                                               "National Coalition",
                                               "Social Democrats",
                                               "True Finns",
                                               "Centre Party",
                                               "Left Alliance",
                                               "Greens",
                                               "Swedish Peoples Party",
                                               "Christian Democrats",
                                               "Movement Now",
                                               "Other"))



# ___ Overall (in both R1 and R2) -----------------------------------------
# NOTE: The variable codes party choice, if a respondent reported vote
#       for the same party in both rounds
BIBU.panel$PartyChoice.Overall                                               <- NA
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  1 & BIBU.panel$Q6_R2 ==  1] <- "National Coalition"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  2 & BIBU.panel$Q6_R2 ==  2] <- "Social Democrats"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  3 & BIBU.panel$Q6_R2 ==  3] <- "True Finns"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  4 & BIBU.panel$Q6_R2 ==  4] <- "Centre Party"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  5 & BIBU.panel$Q6_R2 ==  5] <- "Left Alliance"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  6 & BIBU.panel$Q6_R2 ==  6] <- "Greens"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  7 & BIBU.panel$Q6_R2 == 12] <- "Swedish Peoples Party"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  8 & BIBU.panel$Q6_R2 ==  7] <- "Christian Democrats"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 ==  9 & BIBU.panel$Q6_R2 ==  8] <- "Movement Now"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 == 10 & BIBU.panel$Q6_R2 ==  9] <- "Other"
BIBU.panel$PartyChoice.Overall[BIBU.panel$Q4 == 11 & BIBU.panel$Q6_R2 == 10] <- "Non-voter"

BIBU.panel$PartyChoice.Overall <- factor(BIBU.panel$PartyChoice.Overall, 
                                         levels = c("Non-voter",
                                                    "National Coalition",
                                                    "Social Democrats",
                                                    "True Finns",
                                                    "Centre Party",
                                                    "Left Alliance",
                                                    "Greens",
                                                    "Swedish Peoples Party",
                                                    "Christian Democrats",
                                                    "Movement Now",
                                                    "Other"))




# _ Government vote -------------------------------------------------------
# NOTE: This variable codes whether a person reported voting for
#       a government vs. opposition party


# ___ Round 1 -------------------------------------------------------------
BIBU.panel$GovParty.R1                                                       <- NA
BIBU.panel$GovParty.R1[BIBU.panel$PartyChoice.R1 == "Social Democrats" |
                       BIBU.panel$PartyChoice.R1 == "Centre Party"     |
                       BIBU.panel$PartyChoice.R1 == "Left Alliance"    |
                       BIBU.panel$PartyChoice.R1 == "Greens"           |
                       BIBU.panel$PartyChoice.R1 == "Swedish Peoples Party"] <- "Government voters (Round 1)"
BIBU.panel$GovParty.R1[BIBU.panel$PartyChoice.R1 == "National Coalition"   |
                       BIBU.panel$PartyChoice.R1 == "True Finns"           |
                       BIBU.panel$PartyChoice.R1 == "Christian Democrats"  |
                       BIBU.panel$PartyChoice.R1 == "Movement Now"         |
                       BIBU.panel$PartyChoice.R1 == "Other"]                 <- "Opposition voters (Round 1)"

BIBU.panel$GovParty.R1 <- factor(BIBU.panel$GovParty.R1, 
                                 levels = c("Government voters (Round 1)", 
                                            "Opposition voters (Round 1)"))



# ___ Overall (in both R1 and R2) -----------------------------------------
BIBU.panel$GovParty                                                            <- NA
BIBU.panel$GovParty[BIBU.panel$PartyChoice.Overall == "Social Democrats" |
                    BIBU.panel$PartyChoice.Overall == "Centre Party"     |
                    BIBU.panel$PartyChoice.Overall == "Left Alliance"    |
                    BIBU.panel$PartyChoice.Overall == "Greens"           |
                    BIBU.panel$PartyChoice.Overall == "Swedish Peoples Party"] <- "Government voters"
BIBU.panel$GovParty[BIBU.panel$PartyChoice.Overall == "National Coalition"   |
                    BIBU.panel$PartyChoice.Overall == "True Finns"           |
                    BIBU.panel$PartyChoice.Overall == "Christian Democrats"  |
                    BIBU.panel$PartyChoice.Overall == "Movement Now"         |
                    BIBU.panel$PartyChoice.Overall == "Other"]                 <- "Opposition voters"

BIBU.panel$GovParty <- factor(BIBU.panel$GovParty, 
                              levels = c("Government voters", 
                                         "Opposition voters"))




# _ Left-right ideology ---------------------------------------------------
# QUESTION WORDING: In politics, people sometimes talk about the left and 
#                   the right. Where would you place yourself on a scale 
#                   of zero to ten when zero means left and ten right?
#
#            SCALE: 0: left   ...   10: right
BIBU.panel$LeftRight.R1 <- BIBU.panel$Q5      # Survey round 1
BIBU.panel$LeftRight.R2 <- BIBU.panel$Q8_R2   # Survey round 2




# _ Policy preferences ----------------------------------------------------

# ___ Welfare retrenchment ------------------------------------------------
# NOTE: This policy abolishes the social protection of older workers laid
#       off shortly before reaching the retirement age

# QUESTION WORDING: In December 2020, the Parliament decided to remove 
#                   move the "retirement tube", namely a possibility  
#                   for the elderly unemployed to receive income-based  
#                   unemployment compensation until retirement age. 
#                   What do you think of the decision?
#
#            SCALE: 1: Strongly agree ... 4: Strongly disagree
BIBU.panel$RETIREMENT_TUBE_R1 <- as.numeric(BIBU.panel$RETIREMENT_TUBE_R1)
BIBU.panel$RETIREMENT_TUBE_R2 <- as.numeric(BIBU.panel$RETIREMENT_TUBE_R2)

# Transforming 'Don't know' into NAs
BIBU.panel$RETIREMENT_TUBE_R1[BIBU.panel$RETIREMENT_TUBE_R1 == 5] <- NA
BIBU.panel$RETIREMENT_TUBE_R2[BIBU.panel$RETIREMENT_TUBE_R2 == 5] <- NA

# Calculating the difference
BIBU.panel$RETIREMENT_TUBE_DIFF <- BIBU.panel$RETIREMENT_TUBE_R1 - BIBU.panel$RETIREMENT_TUBE_R2




# ___ Welfare expansion ---------------------------------------------------
# NOTE: This policy extended the compulsory school education from 16 to 18
#       years while providing the needed supplies free of charge.

# QUESTION WORDING: In December 2020, the Parliament decided to raise 
#                   the upper age limit for compulsory schooling to 
#                   18 years What do you think of the decision?
#
#            SCALE: 1: Strongly agree ... 4: Strongly disagree
BIBU.panel$COMPULSORY_SCHOOLING_R1 <- as.numeric(BIBU.panel$COMPULSORY_SCHOOLING_R1)
BIBU.panel$COMPULSORY_SCHOOLING_R2 <- as.numeric(BIBU.panel$COMPULSORY_SCHOOLING_R2)

# Transforming 'Don't know' into NAs
BIBU.panel$COMPULSORY_SCHOOLING_R1[BIBU.panel$COMPULSORY_SCHOOLING_R1 == 5] <- NA
BIBU.panel$COMPULSORY_SCHOOLING_R2[BIBU.panel$COMPULSORY_SCHOOLING_R2 == 5] <- NA

# Calculating the difference
BIBU.panel$COMPULSORY_SCHOOLING_DIFF <- BIBU.panel$COMPULSORY_SCHOOLING_R1 - BIBU.panel$COMPULSORY_SCHOOLING_R2




# ___ Switching the scales ------------------------------------------------
# NOTE: The goal is to make the interpretation more intuitive, i.e.,
#       the higher the number, the larger agreement with the policy
BIBU.panel$RETIREMENT_TUBE_R1 <- (BIBU.panel$RETIREMENT_TUBE_R1*-1)+5
BIBU.panel$RETIREMENT_TUBE_R2 <- (BIBU.panel$RETIREMENT_TUBE_R2*-1)+5

BIBU.panel$COMPULSORY_SCHOOLING_R1 <- (BIBU.panel$COMPULSORY_SCHOOLING_R1*-1)+5
BIBU.panel$COMPULSORY_SCHOOLING_R2 <- (BIBU.panel$COMPULSORY_SCHOOLING_R2*-1)+5




# PANEL STRUCTURE ---------------------------------------------------------
# NOTE: The following step subsets only the relevant variables and organize 
#       them into a long dataset suitable for fixed effects model analysis

# Extracting Round 1 variables
BIBU.panel.long.1 <- data.frame(MEMBERID             = BIBU.panel$MEMBERID,
                                ROUND                = c("Round 1"),
                                PartyChoice.Overall  = BIBU.panel$PartyChoice.Overall,
                                PartyChoice          = BIBU.panel$PartyChoice.R1,
                                GovParty.Overall     = BIBU.panel$GovParty,
                                GovParty.R1          = BIBU.panel$GovParty.R1,
                                LeftRight            = BIBU.panel$LeftRight.R1,
                                LeftRight.R1         = BIBU.panel$LeftRight.R1,
                                RETIREMENT_TUBE      = BIBU.panel$RETIREMENT_TUBE_R1,
                                COMPULSORY_SCHOOLING = BIBU.panel$COMPULSORY_SCHOOLING_R1,
                                WEIGHT               = BIBU.panel$WEIGHT)

# Extracting Round 2 variables 
BIBU.panel.long.2 <- data.frame(MEMBERID             = BIBU.panel$MEMBERID,
                                ROUND                = c("Round 2"),
                                PartyChoice.Overall  = BIBU.panel$PartyChoice.Overall,
                                PartyChoice          = BIBU.panel$PartyChoice.R2,
                                GovParty.Overall     = BIBU.panel$GovParty,
                                GovParty.R1          = BIBU.panel$GovParty.R1,
                                LeftRight            = BIBU.panel$LeftRight.R2,
                                LeftRight.R1         = BIBU.panel$LeftRight.R1,
                                RETIREMENT_TUBE      = BIBU.panel$RETIREMENT_TUBE_R2,
                                COMPULSORY_SCHOOLING = BIBU.panel$COMPULSORY_SCHOOLING_R2,
                                WEIGHT               = BIBU.panel$WEIGHT)

# Merging the two objects together
BIBU.DiD <- rbind(BIBU.panel.long.1, 
                  BIBU.panel.long.2)


# Interim cleanup
rm(BIBU.panel.long.1, BIBU.panel.long.2)


# Adjustments to variable specifications
BIBU.DiD$MEMBERID <- as.factor(BIBU.DiD$MEMBERID)
BIBU.DiD$ROUND <- factor(BIBU.DiD$ROUND, levels = c("Round 1", "Round 2"))
BIBU.DiD$RETIREMENT_TUBE <- as.numeric(BIBU.DiD$RETIREMENT_TUBE)
BIBU.DiD$COMPULSORY_SCHOOLING <- as.numeric(BIBU.DiD$COMPULSORY_SCHOOLING)
BIBU.DiD$LeftRight <- zap_formats(BIBU.DiD$LeftRight)
BIBU.DiD$LeftRight <- labelled::remove_labels(BIBU.DiD$LeftRight)
BIBU.DiD$LeftRight.R1 <- zap_formats(BIBU.DiD$LeftRight)
BIBU.DiD$LeftRight.R1 <- labelled::remove_labels(BIBU.DiD$LeftRight)
BIBU.DiD$WEIGHT <- zap_formats(BIBU.DiD$WEIGHT)
BIBU.DiD$WEIGHT <- labelled::remove_labels(BIBU.DiD$WEIGHT)



# OUTPUT FILE -------------------------------------------------------------

# Saving the output file
save(BIBU.DiD, file = "01-data/BIBU_long.Rda")
save(BIBU.panel, file = "01-data/BIBU_panel.Rda")



# Final cleanup
rm(BIBU.DiD, BIBU.panel)
